package com.haitaoit.chengquan.module.center.activity.info.update;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.model.UserInfoModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class UpdatePhoneActivity2 extends BaseActivity {
    @ViewInject(R.id.phone2_number)
    TextView phone2_number;
    @ViewInject(R.id.phone2_code)
    EditText phone2_code;


    private String code = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_update_phone2);
        setTitle_V("验证手机");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        phone2_number.setText("请输入" + UserInfoModel.getUser_info().getPhone() + "收到的短信验证码");

        // 广播 修改手机绑定成功
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("updatephone_succ");
        registerReceiver(mReceiver, mFilter);

        httpGetCode();
    }

    @OnClick({R.id.getcode_again, R.id.phone_next2})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.getcode_again:// 重新获取
                code = "";
                phone2_code.setText("");
                httpGetCode();
                break;
            case R.id.phone_next2:// 下一步
                if (phone2_code.getText().toString().equals(code)) {
                    startActivity(new Intent(UpdatePhoneActivity2.this, UpdatePhoneActivity3.class));
                } else {
                    ToastUtils.showToast("验证码有误，请重新输入！");
                }
                break;
        }
        super.onClick(v);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    // 获取手机验证码
    private void httpGetCode() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("phone", UserInfoModel.getUser_info().getPhone()));
        list.add(new NameValuePairSign("type", "1"));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("phone", UserInfoModel.getUser_info().getPhone());
        params.addQueryStringParameter("type", "1");
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getupdate_User_phonecode, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            JSONObject js = new JSONObject(Response);
                            code = js.getString("code");
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
